<?php
//error_reporting(E_ALL|E_STRICT);
//ini_set('display_errors', 'on');
class Default_IndexController extends Zend_Controller_Action {


      private $personaDao;


    public function init() {
        $this->_auth = Zend_Auth::getInstance();
      
    }


    public function preDispatch()
    {
       
    }

    public function indexAction() {
//         if (!$this->_auth->hasIdentity()) :
//           return $this->_redirect("/default/index/login/");
//             //echo 'hola';
//        endif;
        
        $datos = $this->getRequest()->getParams();
        
        $auht = Zend_Auth::getInstance();
        
        if ($auht->hasIdentity()){
            $this->view->usuarioSesion = $auht->getIdentity();
            //var_dump($auht->getIdentity()->id);
        } else {
            //var_dump($datos);exit;
            return $this->_redirect("/default/index/login/");
        }
    }
    
    public function loginAction(){
        
        $datos = $this->getRequest()->getParams();
        $tipoLogin = $datos['tipo_login']  ? $datos['tipo_login']*1 : 0;
        
        if($tipoLogin){
            if(!empty($datos['txt_usuario']) && !empty($datos['txt_password'])){
                echo 'hola hola';
                
            } else {
                $this->_redirect("/default/index/nologinadministracion/");
            }
        } else {
            $this->_helper->layout()->setLayout('layout_login');
            if( $this->getRequest()->isPost() ){
                if(!empty($datos['txt_usuario']) && !empty($datos['txt_password'])){

                            $authAdapter = new Zend_Auth_Adapter_DbTable();

                            $authAdapter
                                    ->setTableName('usuario')
                                    ->setIdentityColumn('usuario')
                                    ->setCredentialColumn('clave');

                            $authAdapter
                                    ->setIdentity($datos['txt_usuario'])
                                    ->setCredential($datos['txt_password']);

                            $auth = Zend_Auth::getInstance();

                            $result = $auth->authenticate($authAdapter);

                            if ($result->isValid()) {
                                $usuario = $authAdapter->getResultRowObject(null,'clave');
                                $auth->getStorage()->write($usuario);
                                $this->_redirect('/');

                            } else {
                                $this->view->mensaje = '<div  class="alert alert-error" style="text-align: center;"> Datos Incorrectos</div>';
                            }

                }
            }
        }
       
        
        
    }
    public function loginadministracionAction() {
        $datos = $this->getRequest()->getParams();
        $tipoLogin = $datos['tipo_login']  ? $datos['tipo_login']*1 : 0;
        var_dump($datos);
         
        if($tipoLogin){
            if(!empty($datos['txt_usuario']) && !empty($datos['txt_password'])){
                echo 'hola hola';
                $this->_redirect("/");
                
            } else {
                 $this->view->mensaje = '<div  class="alert alert-error" style="text-align: center;"> Datos Incorrectos</div>';
            }
        }

    }
    public function nologinadministracionAction() {


    }
    public function logoutAction() {
        Zend_Auth::getInstance()->clearIdentity();
        $this->_redirect("/default/index/login/");
        //$this->_helper->redirector('login', 'index');
    }

    public function popupAction() {

        $this->_helper->layout->disableLayout();

    }

    public function listadoAction() {
        $this->_helper->_layout->setLayout('layout_tabs_1');
    }

    public function listado2Action() {

        $this->_helper->_layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        $jsonData = array('page' => $page, 'total' => $total, 'rows' => array());

        $entry = array('persona_id' => 'sdfsd', 'cell' => array('name' => 'sdfsd', 'iso' => 'sdfsd', 'printable_name' => $row['printable_name'], 'iso3' => $row['iso3'], 'numcode' => $row['numcode']),);
        $jsonData['rows'][] = $entry;
        $json = Zend_Json::encode($jsonData);

        echo $json;
    }
    public function pruebaAction() {
       $this->personaDao->pruebaNativoSql();

    }

   



}

